package nc.ui.ct.purdaily.view;

import java.awt.Dimension;

import nc.bs.framework.common.NCLocator;
import nc.desktop.ui.WorkbenchEnvironment;
import nc.funcnode.ui.FuncletInitData;
import nc.funcnode.ui.FuncletWindowLauncher;
import nc.itf.ct.purdaily.IPuLinkCtPrice;
import nc.itf.uap.bbd.func.IFuncRegisterQueryService;
import nc.pubitf.ct.business.IBusinessTypeService;
import nc.sfbase.client.ClientToolKit;
import nc.ui.ct.util.CardEditorHelper;
import nc.ui.ct.view.CtBillForm;
import nc.ui.pub.beans.UIRefPane;
import nc.ui.pub.bill.BillCardPanel;
import nc.ui.pub.bill.BillEditEvent;
import nc.ui.pub.bill.BillItemHyperlinkEvent;
import nc.ui.pub.bill.BillItemHyperlinkListener;
import nc.vo.ct.business.entity.BusinessSetVO;
import nc.vo.ct.business.enumeration.FpricePattern;
import nc.vo.ct.price.entity.AggCtPriceVO;
import nc.vo.ct.price.entity.CtPriceBodyVO;
import nc.vo.ct.purdaily.entity.CtPubillViewVO;
import nc.vo.ct.uitl.ValueUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.sm.funcreg.FuncRegisterVO;




public class PurdailyBillForm extends CtBillForm implements BillItemHyperlinkListener {
	private static final String FUNNODE = "40203025";
	private static final long serialVersionUID = 1L;

	public PurdailyBillForm() {}

	public void afterEdit(BillEditEvent e) {
		String tableCode = getBillCardPanel().getBodyTabbedPane().getSelectedTableCode();
		if ((ValueUtil.equals(tableCode, "pk_ct_pu_change")) || (ValueUtil.equals(tableCode, "pk_ct_pu_exec"))) {
			return;
		}
		super.afterEdit(e);

		String vtrantypecode = (String) getBillCardPanel().getHeadItem("vtrantypecode").getValueObject();
		if("Z2-Cxx-CPHT".equals(vtrantypecode) || "Z2-Cxx-CPHT1".equals(vtrantypecode) ||
				"Z2-Cxx-CPHT3".equals(vtrantypecode)){
			UFDouble ntotalorigmny = (UFDouble) getBillCardPanel().getHeadItem("ntotalorigmny").getValueObject();
			ntotalorigmny = ntotalorigmny == null ? UFDouble.ZERO_DBL : ntotalorigmny;
			int pos = e.getPos();
			if(pos == 0){
				if("ntotalorigmny".equals(e.getKey())){
					getBillCardPanel().setHeadItem("ntotalorigmny", ntotalorigmny);
					getBillCardPanel().setBodyValueAt(ntotalorigmny, 0, "norigtaxmny", "pk_ct_pu_b");
					getBillCardPanel().setBodyValueAt(ntotalorigmny, 0, "ntaxmny", "pk_ct_pu_b");
				}
			} else if(pos == 1){
				if("pk_material".equals(e.getKey()) && "pk_ct_pu_b".equals(tableCode)){
					getBillCardPanel().setHeadItem("ntotalorigmny", ntotalorigmny);
					getBillCardPanel().setBodyValueAt(ntotalorigmny, 0, "norigtaxmny", "pk_ct_pu_b");
					getBillCardPanel().setBodyValueAt(ntotalorigmny, 0, "ntaxmny", "pk_ct_pu_b");
				}
			}
		}
	}

	public void hyperlink(BillItemHyperlinkEvent event) {
		String pk_ct_price = event.getValue().toString();
		AggCtPriceVO priceVO = getCtPriceVO(pk_ct_price);
		if (null == priceVO) {
			return;
		}

		FuncletInitData initData = new FuncletInitData(8, priceVO);
		IFuncRegisterQueryService service = (IFuncRegisterQueryService)NCLocator.getInstance().lookup(IFuncRegisterQueryService.class);

		FuncRegisterVO funvo = null;
		try {
			funvo = service.queryFunctionByCode("40203025");
		}
		catch (BusinessException e) {
			ExceptionUtils.wrappException(e);
		}
		Dimension size = ClientToolKit.getUserClientSize();
		size.setSize(size.width * 0.9D, size.height * 0.7D);
		FuncletWindowLauncher.openFuncNodeDialog(WorkbenchEnvironment.getInstance().getWorkbench(), funvo, initData, null, true, true, size, true);
	}


	public void initUI() {
		super.initUI();
		UIRefPane panel = (UIRefPane)getBillCardPanel().getBodyItem("pk_ct_pu_b", "pk_material").getComponent();
		panel.setMultiSelectedEnabled(true);
		getBillCardPanel().getBodyItem("pk_ct_pu_b", "pk_ct_price").addBillItemHyperlinkListener(this);
	}


	private AggCtPriceVO getCtPriceVO(String pk_ct_price) {
		AggCtPriceVO priceVO = null;
		BillCardPanel panel = getBillCardPanel();
		int[] rows = panel.getBillTable().getSelectedRows();
		CardEditorHelper util = CardEditorHelper.getInstance(panel);
		CtPubillViewVO viewVO = new CtPubillViewVO();
		viewVO.setPk_ct_pu(util.getHeadStringValue("pk_ct_pu"));
		viewVO.setPk_org(util.getHeadStringValue("pk_org"));
		viewVO.setNqtorigtaxprice(util.getBodyUFDoubleValue(rows[0], "nqtorigtaxprice"));

		viewVO.setPk_ct_price(pk_ct_price);
		IPuLinkCtPrice linkCtPrice = (IPuLinkCtPrice)NCLocator.getInstance().lookup(IPuLinkCtPrice.class);
		try {
			priceVO = linkCtPrice.getCtPriceVO(viewVO);
		}
		catch (BusinessException e) {
			ExceptionUtils.wrappException(e);
			return priceVO;
		}
		return priceVO;
	}


	private BusinessSetVO queryBusinessSetVO(String ctrantypeid) {
		try {
			IBusinessTypeService service = (IBusinessTypeService)NCLocator.getInstance().lookup(IBusinessTypeService.class);

			return service.queryBusinessVO(ctrantypeid);
		}
		catch (BusinessException e) {
			ExceptionUtils.wrappException(e); }
		return null;
	}


	private void setPriceVO(AggCtPriceVO priceVO, String ctrantypeid) {
		priceVO.setIsFromPurdaily(UFBoolean.TRUE);
		BusinessSetVO businessVO = queryBusinessSetVO(ctrantypeid);

		if (null != businessVO) {
			if (FpricePattern.MULTIPRICE.value().equals(businessVO.getFpricePattern())) {
				priceVO.setIsMultiPriceType(UFBoolean.TRUE);
			}
			if (FpricePattern.ALLPRICE.value().equals(businessVO.getFpricePattern())) {
				for (CtPriceBodyVO body : priceVO.getChildrenVO()) {
					body.setTotalprice(body.getBaseprice());
					body.setBaseprice(null);
				}
			}
		}
	}
}